import { Random } from "mockjs";
import type { TreeLike } from "@/models/tree-like";

export function randomTree(depth: number = 3, split: number = 3) {
  const tree: TreeLike[] = [];
  const root: TreeLike = { id: Random.id(), name: "0-" + Random.cname() };
  tree.push(root);

  let _depth = 0;
  function gen(ids: Array<string | number>) {
    if (_depth < depth) {
      _depth++;

      const level: TreeLike[] = [];
      for (const id of ids) {
        const _split = Random.integer(_depth === 1 ? 1 : 0, split);

        for (let i = 0; i < _split; i++) {
          const item: TreeLike = { id: Random.id(), name: _depth + "-" + Random.cname(), pid: id };
          level.push(item);
        }
      }

      tree.push(...level);

      gen(level.map((t) => t.id));
    }
  }

  gen([root.id]);

  return tree;
}

export function randomTrees(count: number = 2, depth: number = 3, split: number = 3) {
  const _count = Random.integer(1, count);
  const trees: TreeLike[] = [];
  for (let i = 0; i < _count; i++) {
    trees.push(...randomTree(depth, split));
  }
  return trees;
}
